package com.jml.spring.aop注解事务.TransactionUtils;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;


@Component
@Scope("prototype")
public class TransactionUtils {

	//全局接收事务状态
	private TransactionStatus transactionStatus ;

	//获取事务源
	@Autowired
	private DataSourceTransactionManager dataSourceTransactionManager;

	//开启事务
	public TransactionStatus begin(){
		//得到默认事务传播级别
		transactionStatus = dataSourceTransactionManager.getTransaction(new DefaultTransactionAttribute());
		return transactionStatus;
	}

	//提交事务
	public void commit(TransactionStatus transactionStatus){
		dataSourceTransactionManager.commit(transactionStatus);
	}

	//回滚事务
	public void rollback(){
		dataSourceTransactionManager.rollback(transactionStatus);
	}

}
